package com.example.sls.dao.social;

import com.example.sls.pojo.social.TGroups;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 群组表(TGroups)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:55:05
 */
public interface TGroupsDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from t_groups where id=#{id}")
  TGroups queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param tGroups  查询条件
   * @param pageSize 分页对象
   * @param offset   偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from t_groups where 1=1" +
          " and (id=#{tGroups.id} or (isnull(#{tGroups.id}) or #{tGroups.id}=0))" +
          " and (name=#{tGroups.name} or (isnull(#{tGroups.name}) or #{tGroups.name}=''))" +
          " and (description=#{tGroups.description} or (isnull(#{tGroups.description}) or #{tGroups.description}=''))" +
          " and (created_at=#{tGroups.createdAt} or (isnull(#{tGroups.createdAt}) or #{tGroups.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<TGroups> queryAllByLimit(TGroups tGroups, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from t_groups")
  long count();

  /**
   * 新增数据
   *
   * @param tGroups 实例对象
   * @return 影响行数
   */
  @Insert("insert into t_groups (id,name,description,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{name}!='' and !isnull(#{name}),#{name},null)," +
          "if(#{description}!='' and !isnull(#{description}),#{description},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(TGroups tGroups);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param tGroups 实例对象
   * @return 影响行数
   */
  @Update("update t_groups set " +
          "name=if(isnull(#{name}) or #{name}='',name,#{name})," +
          "description=if(isnull(#{description}) or #{description}='',description,#{description})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(TGroups tGroups);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from t_groups where id=#{id}")
  int deleteById(Integer id);
}

